package com.wyp168.jse.deepfirst;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName StudyDeepFirst
 * @Title TODO
 * @Description TODO
 * @Author wyp168
 * @Date 2023/3/10 22:23
 */
public class StudyDeepFirst {
    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 4, 5};
        boolean[] isExists = new boolean[nums.length];
        StudyDeepFirst solution = new StudyDeepFirst();
        List<List<Integer>> resultList = new ArrayList<>();
        solution.deepFirst(nums, new ArrayList<>(), isExists, resultList);

        solution.resultList.forEach(list -> {
            list.forEach(System.out::print);
            System.out.println();
        });
    }

    private final List<List<Integer>> resultList = new ArrayList<>();

    public void deepFirst(int[] nums, List<Integer> tempList, boolean[] isExists, List<List<Integer>> resultList) {
        if (tempList.size() == nums.length) {
            resultList.add(tempList);
        } else {
            for(int i = 0; i < nums.length; i ++) {
                if (!isExists[i]) {
                    tempList.add(nums[i]);
                    isExists[i] = true;
                    deepFirst(nums, tempList, isExists, resultList);
                    isExists[i] = false;
                    tempList.remove(nums[i]);
                }
            }
        }
    }
}
